﻿using System.Configuration;

namespace Altairis.Web.Management.Configuration {

    public enum UrlRewritingAction {
        None,
        Rewrite,
        Redirect,
        PermanentRedirect
    }

    public enum UrlRewritingStage {
        BeginRequest,
        PostAuthorizeRequest
    }

    public class UrlRewritingElement : ConfigurationElement {

        [ConfigurationProperty("enabled", IsRequired = false, DefaultValue = false)]
        public bool Enabled {
            get { return (bool)this["enabled"]; }
            set { this["enabled"] = value; }
        }

        [ConfigurationProperty("ExcludeSystemUrls", IsRequired = false, DefaultValue = true)]
        public bool ExcludeSystemUrls {
            get { return (bool)this["ExcludeSystemUrls"]; }
            set { this["ExcludeSystemUrls"] = value; }
        }

        [ConfigurationProperty("stage", IsRequired = false, DefaultValue = UrlRewritingStage.PostAuthorizeRequest)]
        public UrlRewritingStage Stage {
            get { return (UrlRewritingStage)this["stage"]; }
            set { this["stage"] = value; }
        }

        [ConfigurationProperty("rules", IsRequired = false)]
        public UrlRewritingRuleElementCollection Rules {
            get { return (UrlRewritingRuleElementCollection)this["rules"]; }
            set { this["rules"] = value; }
        }

    }

    public class UrlRewritingRuleElement : ConfigurationElement {

        [ConfigurationProperty("from", IsRequired = true, DefaultValue = "")]
        public string From {
            get { return (string)this["from"]; }
            set { this["from"] = value; }
        }

        [ConfigurationProperty("to", IsRequired = false, DefaultValue = "")]
        public string To {
            get { return (string)this["to"]; }
            set { this["to"] = value; }
        }

        [ConfigurationProperty("caseSensitive", IsRequired = false, DefaultValue = false)]
        public bool CaseSensitive {
            get { return (bool)this["caseSensitive"]; }
            set { this["caseSensitive"] = value; }
        }

        [ConfigurationProperty("action", IsRequired = false, DefaultValue = UrlRewritingAction.Rewrite)]
        public UrlRewritingAction Action {
            get { return (UrlRewritingAction)this["action"]; }
            set { this["action"] = value; }
        }

        [ConfigurationProperty("mapContextItems", IsRequired = false)]
        public UrlRewritingMapElementCollection MapContextItems {
            get { return (UrlRewritingMapElementCollection)this["mapContextItems"]; }
            set { this["mapContextItems"] = value; }
        }

    }

    public class UrlRewritingRuleElementCollection : ConfigurationElementCollection {

        protected override ConfigurationElement CreateNewElement() {
            return new UrlRewritingRuleElement();
        }

        protected override object GetElementKey(ConfigurationElement element) {
            return (element as UrlRewritingRuleElement).From;
        }

    }

    public class UrlRewritingMapElement : ConfigurationElement {

        [ConfigurationProperty("key", IsRequired = true, DefaultValue = "")]
        public string Key {
            get { return (string)this["key"]; }
            set { this["key"] = value; }
        }

        [ConfigurationProperty("value", IsRequired = true, DefaultValue = "")]
        public string Value {
            get { return (string)this["value"]; }
            set { this["value"] = value; }
        }

    }

    public class UrlRewritingMapElementCollection : ConfigurationElementCollection {
        protected override ConfigurationElement CreateNewElement() {
            return new UrlRewritingMapElement();
        }

        protected override object GetElementKey(ConfigurationElement element) {
            return (element as UrlRewritingMapElement).Key;
        }
    }

}
